package com.glinsoft.ticktak.controller.common;


import io.swagger.annotations.ApiModel;
import net.sf.json.JSONObject;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


/**
 * 用于Java数据类型对象转化为JSON字符串格式输出
 */
@SuppressWarnings("serial")
@ApiModel("1：成功，内含提示消息；0: 成功；-1：失败；-3：无权限；-4：应用内部错误；")
public class JSON<T> implements Serializable {


    public static <T> JSON<T> OK(T data){
        JSON json = new JSON();
        json.setCode(0);
        json.setMsg("ok");
        json.setData(data);
        return json;
    }

    public static <T> JSON<T> ERROR(T data){
        JSON json = new JSON();
        json.setCode(-1);
        json.setMsg("error");
        json.setData(data);
        return json;
    }

    public static <T> JSON<T> DATA(int code,String msg,T data){
        JSON json = new JSON();
        json.setCode(code);
        json.setMsg(msg);
        json.setData(data);
        return json;
    }

    private JSON(){

    }



    @Override
    public String toString(){
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
        jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
        jsonConfig.registerJsonValueProcessor(java.sql.Time.class, new JsonTimeValueProcessor());
        jsonConfig.registerJsonValueProcessor(LocalDate.class, new LocalDateValueProcessor());
        jsonConfig.registerJsonValueProcessor(LocalDateTime.class, new LocalDateTimeValueProcessor());
        JSONObject jsonObj = JSONObject.fromObject(this,jsonConfig);
        return jsonObj.toString();
    }

    private int code;

    private String msg;

    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}



class JsonTimeValueProcessor implements JsonValueProcessor {
    private String format = "HH:mm:ss";
    public JsonTimeValueProcessor() {
        super();
    }
    public JsonTimeValueProcessor(String format) {
        super();
        this.format = format;
    }
    @Override
    public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    @Override
    public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    private Object process(Object value) {
        if (value instanceof java.sql.Time) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }

}

class JsonDateValueProcessor implements JsonValueProcessor {
    private String format = "yyyy-MM-dd HH:mm:ss";

    public JsonDateValueProcessor() {
        super();
    }

    @Override
    public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    @Override
    public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    private Object process(Object value) {
        if (value instanceof java.util.Date || value instanceof java.sql.Date) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(value);
        }
        return value == null ? "" : value.toString();
    }

}


class LocalDateValueProcessor implements JsonValueProcessor {
    public LocalDateValueProcessor() {
        super();
    }

    @Override
    public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    @Override
    public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    private Object process(Object value) {
        if (value instanceof LocalDate) {
            return ((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return value == null ? "" : value.toString();
    }

}

class LocalDateTimeValueProcessor implements JsonValueProcessor {
    public LocalDateTimeValueProcessor() {
        super();
    }

    @Override
    public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    @Override
    public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) {
        return process(paramObject);
    }

    private Object process(Object value) {
        if (value instanceof LocalDate) {
            return ((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        return value == null ? "" : value.toString();
    }

}
